#region Namespaces

using System;
using System.Linq;
using System.Xml;

#endregion

namespace Vibstudio.X4NET.Xml.Linq
{
    public class XText : XNode
    {
        private string _value;

        public XText(string value)
        {
            _value = value;
        }

        public XText(XText other)
        {
            _value = other._value;
        }

        public override XmlNodeType NodeType
        {
            get { return XmlNodeType.Text; }
        }

        public string Value
        {
            get { return _value; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                _value = value;
            }
        }

        public override void WriteTo(XmlWriter writer)
        {
            if (Value.Length > 0 && Value.All(c => c == ' ' || c == '\t' || c == '\r' || c == '\n'))
            {
                writer.WriteWhitespace(_value);
            }
            else
            {
                writer.WriteString(_value);
            }
        }
    }
}